package com.yyfax.ph.personal.update;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.List;

public class UpdateModel extends Model {
	private static final String TAG = UpdateModel.class.getSimpleName();

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		UpdateResult result = cast(obj);
		EventAgent.post(result);
	}

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		return service.checkUpdate(token, new EmptyEntity());
	}

	public interface Service {
		@POST("sys/version/get")
		Observable<UpdateResult> checkUpdate(@Header("token") String token, @Body EmptyEntity params);
	}

	public static class UpdateResult extends BaseResultEntity<UpdateBean> {

	}

	public static class UpdateBean {

		public String type;
		public String version;
		public String url;
		public String content;
		public int yesNo;
		public String configVersion;
		public int enableAll;
		public int versionCode;
		public List<String> channels;
	}

}
